/** Project : Sticky Notes Plus */
/** Author  : Sainagakishore Srikantham */
/** Email   : ssn.kishore@gmail.com */

/** File    : StickyNotesDataMgr.cpp */
/** Purpose : This file contains the definition for the class CStickyNotesDataMgr which is responsible to deal
              with saving the "sticky notes" created by the user on to the disk */

#include "StickyNotesDataMgr.h"

#include <fstream>

using std::fstream;
using std::ios;
using std::string;

/** Constructor of the class */
CStickyNotesDataMgr::CStickyNotesDataMgr()
{
}

/** Creates a new note of the specified type */
void CStickyNotesDataMgr::AddNote(NoteType aNoteType, bool aBeingReloaded)
{
    switch(aNoteType)
    {
    case STICKY_EXPIRY_IN_HOURS:
        {
            break;
        }
    case STICKY_EXPIRY_ON_DATE:
        {
            break;
        }
    case STICKY_GENERIC_NOTE:
        {
            int vectorSize = iStickyNotes.size();
            iStickyNotes.push_back(new CStickyNote(vectorSize + 1, aBeingReloaded));
            break;
        }
    }
}

/** Saves all notes to a file */
void CStickyNotesDataMgr::SaveAllNotes()
{
    fstream binaryFile("StickyNotesPlus.dat",ios::out|ios::binary);

    for(int index=0; index < iStickyNotes.size(); ++index)
    {
        StickyNoteStruct noteStruct;
        CStickyNote* stickyNote = iStickyNotes.at(index);

        noteStruct.noteId = stickyNote->GetNoteId();
        string temp = stickyNote->GetTitle().toStdString();
        temp.copy(noteStruct.title, temp.length(), 0);
        noteStruct.title[temp.length()] = '\0';

        temp = "";
        temp = stickyNote->GetMsg().toStdString();

        temp.copy(noteStruct.message, temp.length(), 0);
        noteStruct.message[temp.length()] = '\0';

        noteStruct.type = stickyNote->GetNoteType();

        switch(stickyNote->GetNoteType())
        {
        case STICKY_EXPIRY_IN_HOURS:
            {
                break;
            }
        case STICKY_EXPIRY_ON_DATE:
            {
                break;
            }
        default:
            break;
        }

        binaryFile.write(reinterpret_cast<char*>(&noteStruct),sizeof(StickyNoteStruct));
    }

    binaryFile.close();
}

/** Saves the specified note to the file */
void CStickyNotesDataMgr::SaveNote(int aNoteId)
{
    // TBD
}

/** Deletes the specified note */
void CStickyNotesDataMgr::DeleteNote(int aNoteId)
{
    // TBD
}

/** Reloads the saved notes */
void CStickyNotesDataMgr::ReloadNotes()
{
    fstream binaryFile("StickyNotesPlus.dat",ios::in|ios::binary);

    if(!binaryFile.fail())
    {
        do
        {
            StickyNoteStruct noteStruct;
            binaryFile.read(reinterpret_cast<char*>(&noteStruct),sizeof(StickyNoteStruct));
            if(binaryFile.eof())
                break;

            AddNote(noteStruct.type, true);
            CStickyNote* stickyNote = iStickyNotes.at(iStickyNotes.size() - 1);

            QString tempTitle = noteStruct.title;
            stickyNote->SetTitle(tempTitle);

            QString tempMsg = noteStruct.message;
            stickyNote->SetMsg(tempMsg);

            stickyNote->SetNoteType(noteStruct.type);
        }while(!binaryFile.eof());
    }
    binaryFile.close();
}

/** Returns an iterator for the Sticky Notes Vector */
vector<CStickyNote*>::iterator CStickyNotesDataMgr::GetNotesBeginIterator()
{
    return iStickyNotes.begin();
}

/** Returns an iterator for the Sticky Notes Vector end */
vector<CStickyNote*>::iterator CStickyNotesDataMgr::GetNotesEndIterator()
{
    return iStickyNotes.end();
}
